library(stargazer); library(ggplot2)

########
##Data loading

setwd('INSERT WORKING DIRECTORY HERE')
#Respondents dataset
ep<-read.csv('eplo_pool.csv',stringsAsFactors = F)
#Participant pool dataset
ep2<-read.csv('eplo_respondents.csv',stringsAsFactors = F)

########
##Data processing

#Sex
ep2$woman<-ifelse(ep2$a1_gender=="",NA,ifelse(
  ep2$a1_gender=='Man',0,1))

#Age
ep2$age<-2023-as.numeric(ep2$a3_yob)

#Education
ep2$post_college<-ifelse(ep2$a2_educ%in%c('Doctorate','Master’s degree or professional degree'),1,0)

#Majority status
ep2$majority_status<-ifelse(ep2$a7_coalition=="",NA,ep2$a7_coalition)
ep2$in_majority<-ifelse(ep2$majority_status=='Member of the governing coalition',1,0)

#Right/Wrong direction
ep2$direction<-ifelse(ep2$a8_direction=='',NA,ep2$a8_direction)
ep2$right_direction<-ifelse(ep2$direction=='Right direction',1,0)

#L/R
ep2$lr<-as.numeric(ifelse(ep2$e4_left_right=='Left\n0\n',0,
                          ifelse(ep2$e4_left_right=='Right\n10\n',10,ep2$e4_left_right)))
#Mayor
ep2$mayor<-ifelse(ep2$a6_position=='Mayor / council head or equivalent',1,0)

#Party
ep2$spd<-ifelse(ep2$c_party_id_de=='SPD - Sozialdemokratische Partei Deutschlands',1,
                ifelse(ep2$c_party_id_de!='',0,NA))
ep2$lab<-ifelse(ep2$c_party_id_uk=='Labour Party',1,
                ifelse(ep2$c_party_id_uk!='',0,NA))


##############################
##############################
# # # Figure 1: Main results
##############################
##############################
ep$country_condition<-paste(ep$country,ep$letter)
#Calculating response rates by treatment condition, by country and in pooled sample
rrt<-data.frame(country=factor(c(rep('Germany',2),
                                 rep('United Kingdom',2),rep('Pooled',2)),levels=c('Germany','United Kingdom','Pooled')),
                condition=rep(c('Email','Letter'),3),
                rr=c(tapply(ep$resp, ep$country_condition, mean, na.rm=T),
                     tapply(ep$resp, ep$letter, mean, na.rm=T))*100)

#OLS estimates of letter effects
mp<-lm(resp~letter+country,ep)
muk<-lm(resp~letter,ep[ep$country=='uk',])
mde<-lm(resp~letter,ep[ep$country=='de',])

#Code to produce Figure 1
s1gg<-ggplot(data=rrt, aes(x=country, y=rr,fill=condition)) + 
  geom_bar(stat="identity", 
           position="dodge", width=.5)+
  ylim(0,40) +
  ylab('Response Rate')+xlab('')+ 
  theme_bw()+
  #Change axis font
  theme(axis.text.x = element_text(size = 11),
        #Edit legend legend
        legend.position = c(0.5, 0.9),
        legend.title = element_blank(),
        legend.text = element_text(size=11))+
  guides(fill=guide_legend(ncol=2))+
  #Change bar colors
  scale_fill_manual(values = rep(c("darkviolet","seagreen"),7))+
  #Add vertical line to separate pooled estimate
  geom_vline(xintercept=6.5,linetype=2) +
  #Bold pooled
  scale_x_discrete(labels=c("Pooled"=expression(bold(Pooled)), parse=TRUE))

#Adding ATEs
s1_coefs<-round(c(coef(mde)[2],coef(muk)[2],coef(mp)[2])*100,1)
lbs<-as.character(round(as.vector(s1_coefs),2))
lbs<-paste(lbs,'**',sep='')

s1gg+                             
  # Add ATEs to plot
  annotate('text', x = 1:3, y = c(26,9,14), 
           label = lbs)

##############################
##############################
# # # Table 1: Letter/Email Respondents
##############################
##############################

#Mean, p-values, and sample sizes reported in Table 1

t.test(ep2$pop[ep2$letter==0],ep2$pop[ep2$letter==1])
length(na.omit(ep2$pop))

t.test(ep2$age[ep2$letter==0],ep2$age[ep2$letter==1])
length(na.omit(ep2$age))

t.test(ep2$woman[ep2$letter==0],ep2$woman[ep2$letter==1])
length(na.omit(ep2$woman))

t.test(ep2$post_college[ep2$letter==0],ep2$post_college[ep2$letter==1])
length(na.omit(ep2$post_college))

t.test(ep2$mayor[ep2$letter==0],ep2$mayor[ep2$letter==1])
length(na.omit(ep2$mayor))

t.test(ep2$in_majority[ep2$letter==0],ep2$in_majority[ep2$letter==1])
length(na.omit(ep2$in_majority))

t.test(ep2$lr[ep2$letter==0],ep2$lr[ep2$letter==1])
length(na.omit(ep2$lr))

t.test(ep2$lab[ep2$letter==0],ep2$lab[ep2$letter==1])
length(na.omit(ep2$lab))

t.test(ep2$spd[ep2$letter==0],ep2$spd[ep2$letter==1])
length(na.omit(ep2$spd))


##############################
##############################
# # # Table 2: Recruitment mode and response quality
##############################
##############################
ep2$completed<-ifelse(ep2$Finished,1,0)
ep2$mins<-ep2$Duration..in.seconds./60

#OLS estimates of letter on response quality
q1<-lm(Progress~letter+country,ep2)
q2<-lm(completed~letter+country,ep2)
q3<-lm(mins~letter+country,ep2[ep2$mins<120,])

#Code to produce Table 2
stargazer(q1,q2,q3,no.space = T)



##############################
##############################
# # # Table A1: Balance test
##############################
##############################

#Pre-processing
ep$councillor<-ifelse(ep$role=='Councillor',1,0)
ep$conservative_party<-ifelse(grepl('onservat',ep$party),1,0)
ep$labour_party<-ifelse(grepl('abour',ep$party),1,0)
ep$spd<-ifelse(grepl('SPD',ep$party),1,0)
ep$cdu<-ifelse(grepl('CDU',ep$party),1,
               ifelse(ep$party=='CSU',1,0))

#T-tests, means, and Ns reported in Table A1 
t.test(ep$woman[ep$letter==0],ep$woman[ep$letter==1])
length(na.omit(ep$woman))

t.test(ep$population[ep$letter==0],ep$population[ep$letter==1])
length(na.omit(ep$population))

t.test(ep$councillor[ep$letter==0],ep$councillor[ep$letter==1])
length(na.omit(ep$councillor))

t.test(ep$labour_party[ep$letter==0&ep$country=='uk'],
       ep$labour_party[ep$letter==1&ep$country=='uk'])
length(na.omit(ep$labour_party[ep$country=='uk']))

t.test(ep$conservative_party[ep$letter==0&ep$country=='uk'],
       ep$conservative_party[ep$letter==1&ep$country=='uk'])
length(na.omit(ep$conservative_party[ep$country=='uk']))

t.test(ep$cdu[ep$letter==0&ep$country=='de'],
       ep$cdu[ep$letter==1&ep$country=='de'])
length(na.omit(ep$cdu[ep$country=='de']))

t.test(ep$spd[ep$letter==0&ep$country=='de'],
       ep$spd[ep$letter==1&ep$country=='de'])
length(na.omit(ep$spd[ep$country=='de']))

